package com.chencs.app.config.controller;

import com.alibaba.fastjson.JSON;
import com.chencs.app.AppCommon;
import com.chencs.app.config.service.AppConfigService;
import com.chencs.vo.net.request.AdviseRequest;
import com.chencs.vo.net.request.ProductRequest;
import com.chencs.vo.net.request.RequestBase;
import com.chencs.vo.net.response.TagResponse;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by fangzehua on 2017/7/12.
 */

@RestController
@RequestMapping("/app/config")
public class AppConfigController {

    @Autowired
    private AppConfigService appConfigService;

    @RequestMapping(value = "/checkVersion", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String checkVersion(@RequestBody RequestBase request){
        return JSON.toJSONString(appConfigService.checkVersion(request));

    }

    @RequestMapping(value = "/getConfig", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String getConfig(@RequestBody RequestBase request){
        return JSON.toJSONString(appConfigService.getCofnig(request));

    }

    @RequestMapping(value = "/addAdvise", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String addAdvise(@RequestBody AdviseRequest request){
        return JSON.toJSONString(appConfigService.addAdvise(request));

    }


    @RequestMapping(value = "/relation", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    public String relation(@RequestBody ProductRequest request){
        org.jsoup.nodes.Document res;
        TagResponse response = new TagResponse();
        try {
            res = Jsoup.connect("https://suggest.taobao.com/sug?code=utf-8&q="+request.getKey()).timeout(10000).ignoreContentType(true).post();
            String str = res.body().text();
            response = JSON.parseObject(str,TagResponse.class);
        }catch (Exception e){
            e.printStackTrace();
            response.setCode(AppCommon.ERROR);
        }

        return JSON.toJSONString(response);

    }



}
